package com.next.muscu.visual;

import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.next.muscu.data.Exo;
import com.next.muscu.data.ExoType;
import com.next.muscu.data.GenericListModel;
import com.next.muscu.data.MyDataObject;
import com.next.muscu.data.ObservableList;

public class VisualExoSimplePanel extends AbstractVisualBasicEditorPanel implements IVisualBasicEditorPanel {

	Exo data;
	JTextField codeTextField = new JTextField();
	JTextField descriptionTextField = new JTextField();
	JTextField idTextField = new JTextField();
	JTextField pdfTextField = new JTextField();
	JComboBox ExoTypeComboBox = new JComboBox();
	ObservableList<ExoType>  comboData  = new ObservableList<ExoType>("from ExoType");
	
	public VisualExoSimplePanel(Exo data) {
		super();
		this.data = data;
		buildGui();
	}

	public VisualExoSimplePanel() {
		super();
		buildGui();
	}
	
	private void buildGui()
	{
		ExoTypeComboBox.setRenderer(new ExoTypeJlistCellRender());
		
		GenericListModel<ExoType> comboModel = new GenericListModel(comboData); 
		ExoTypeComboBox.setModel(comboModel);
		codeTextField.setEditable(false);
		this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
		this.add(Box.createRigidArea(new Dimension(20,0)));
		this.add(new JLabel("id : "));
		this.add(Box.createRigidArea(new Dimension(5,0)));
		this.add(idTextField);
		this.add(Box.createRigidArea(new Dimension(5,0)));
		this.add(new JLabel("Type : "));
		this.add(Box.createRigidArea(new Dimension(20,0)));
		this.add(ExoTypeComboBox);
		this.add(Box.createRigidArea(new Dimension(5,0)));
		this.add(new JLabel("code : "));
		this.add(Box.createRigidArea(new Dimension(5,0)));
		this.add(codeTextField);
		this.add(Box.createRigidArea(new Dimension(20,0)));
		this.add(new JLabel("Nom : "));
		this.add(Box.createRigidArea(new Dimension(5,0)));
		this.add(descriptionTextField);		
		this.add(Box.createRigidArea(new Dimension(20,0)));
		this.add(new JLabel("PDF : "));
		this.add(Box.createRigidArea(new Dimension(5,0)));
		this.add(pdfTextField);	
	}

	public Exo getData() {
		this.data.setCode(codeTextField.getText());
		this.data.setDescription(descriptionTextField.getText());
		this.data.setId(Integer.parseInt(idTextField.getText()));
		this.data.setExoType((ExoType) ExoTypeComboBox.getSelectedItem());
		this.data.setPdfFileName(pdfTextField.getText());
		return data;
	}

	public void setData(Exo data) {
		this.data = data;
		setGui();
	}

	public void setData(MyDataObject data) {
		this.data = (Exo) data;
		setGui();
	}
	private void setGui()
	{
		this.idTextField.setText(data.getId().toString());
		this.codeTextField.setText(data.getCode());
		this.descriptionTextField.setText(data.getDescription());
		ExoType e = data.getExoType();
		this.ExoTypeComboBox.setSelectedItem(e);
		pdfTextField.setText(data.getPdfFileName());
		this.validate();
	}
}
